from sqlalchemy import (
    Column, BigInteger, String, Boolean, DateTime, Text, Date, 
    Enum, Integer, ForeignKey, JSON, DECIMAL
)
from sqlalchemy.sql import func
from sqlalchemy.orm import relationship
from ..database import Base
import enum


class GenderEnum(enum.Enum):
    male = "male"
    female = "female"
    other = "other"


class User(Base):
    __tablename__ = "users"

    id = Column(BigInteger, primary_key=True, index=True, comment='用户ID')
    username = Column(
        String(50), unique=True, index=True, nullable=False, comment='用户名'
    )
    email = Column(
        String(100), unique=True, index=True, nullable=False, comment='邮箱'
    )
    password_hash = Column(String(255), nullable=False, comment='密码哈希')
    nickname = Column(String(100), nullable=True, comment='昵称')
    avatar = Column(String(255), nullable=True, comment='头像URL')
    bio = Column(Text, nullable=True, comment='个人简介')
    phone = Column(String(20), nullable=True, comment='手机号')
    gender = Column(Enum(GenderEnum), nullable=True, comment='性别')
    birthday = Column(Date, nullable=True, comment='生日')
    location = Column(String(100), nullable=True, comment='所在地')
    last_login_at = Column(
        DateTime(timezone=True), 
        server_default=func.now(), 
        onupdate=func.now(),
        comment='最后登录时间'
    )
    login_count = Column(Integer, default=0, comment='登录次数')
    created_at = Column(
        DateTime(timezone=True), 
        server_default=func.now(),
        comment='创建时间'
    )
    updated_at = Column(
        DateTime(timezone=True), 
        server_default=func.now(),
        onupdate=func.now(),
        comment='更新时间'
    )
    is_active = Column(Boolean, default=True, comment='是否激活')
    is_verified = Column(Boolean, default=False, comment='是否已验证邮箱')
    
    # 关系
    preferences = relationship(
        "UserPreference", 
        back_populates="user", 
        uselist=False, 
        cascade="all, delete-orphan"
    )
    
    chat_sessions = relationship(
        "ChatSession",
        back_populates="user",
        cascade="all, delete-orphan"
    )
    
    def __repr__(self):
        return (
            f"<User(id={self.id}, username='{self.username}', "
            f"email='{self.email}')>"
        )


class UserPreference(Base):
    __tablename__ = "user_preferences"

    id = Column(BigInteger, primary_key=True, index=True, comment='偏好ID')
    user_id = Column(
        BigInteger, 
        ForeignKey('users.id', ondelete='CASCADE'), 
        nullable=False, 
        unique=True, 
        comment='用户ID'
    )
    favorite_avatar_id = Column(BigInteger, nullable=True, comment='喜欢的数字人ID')
    favorite_novels = Column(JSON, nullable=True, comment='喜欢的小说列表')
    favorite_characters = Column(JSON, nullable=True, comment='喜欢的角色列表')
    language = Column(String(10), default='zh-CN', comment='语言偏好')
    theme = Column(String(20), default='light', comment='主题偏好')
    voice_enabled = Column(Boolean, default=True, comment='是否启用语音')
    voice_speed = Column(DECIMAL(3, 1), default=1.0, comment='语音播放速度')
    auto_play_voice = Column(Boolean, default=True, comment='是否自动播放语音')
    notification_enabled = Column(Boolean, default=True, comment='是否启用通知')
    privacy_settings = Column(JSON, nullable=True, comment='隐私设置')
    created_at = Column(
        DateTime(timezone=True), 
        server_default=func.now(),
        comment='创建时间'
    )
    updated_at = Column(
        DateTime(timezone=True), 
        server_default=func.now(),
        onupdate=func.now(),
        comment='更新时间'
    )
    
    # 关系
    user = relationship("User", back_populates="preferences")
    
    def __repr__(self):
        return f"<UserPreference(id={self.id}, user_id={self.user_id})>" 